package com.bjpowernode.crm.settings.web.controller;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.DicType;
import com.bjpowernode.crm.settings.domain.DicValue;
import com.bjpowernode.crm.settings.service.DicTypeService;
import com.bjpowernode.crm.settings.service.DicValueService;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName : DicTypeController
 * @Author : 司猫子
 * @Date: 2020/12/25 15:50
 */
@Controller
public class DictionaryController {
    @Autowired
    private DicTypeService dicTypeService;

    @Autowired
    private DicValueService dicValueService;
    @RequestMapping("/settings/dictionary/index.do")
    public String toDictionary(){
        return "settings/dictionary/index";
    }
    @RequestMapping("/settings/dictionary/type/index.do")
    public String toDictionaryType(Model model){
        List<DicType> dicTypes = dicTypeService.selectAllDicTypes();
        model.addAttribute("dicTypeList", dicTypes);
        return "settings/dictionary/type/index";
    }
    @RequestMapping("/settings/dictionary/value/index.do")
    public String toDictionaryValue(Model model){
        List<DicValue> dicValues = dicValueService.selectAllDicValues();
        model.addAttribute("dicValueList",dicValues);
        return "settings/dictionary/value/index";
    }
    @RequestMapping("/settings/dictionary/type/toSave.do")
    public String toSaveType(){
        return "settings/dictionary/type/save";
    }
    @RequestMapping("/settings/dictionary/type/saveCreateDicType.do")
    @ResponseBody
    public Object saveType(DicType dicType){
        int i = dicTypeService.insertDicType(dicType);
        ReturnObject returnObject = new ReturnObject();
        if(i == 0){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("数据添加失败");
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        }
        return returnObject;
    }
    @RequestMapping("/settings/dictionary/type/checkCode.do")
    @ResponseBody
    public Object checkCode(String code){
        ReturnObject returnObject = new ReturnObject();
        DicType dicType = dicTypeService.selectDicTypeByCode(code);
        if(dicType != null){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("编码已存在");
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        }
        return returnObject;
    }
    @RequestMapping("/settings/dictionary/type/deleteDicTypeByCodes.do")
    @ResponseBody
    public Object delDicTypeByCodes(String[] code){
        ReturnObject returnObject = new ReturnObject();
        int i = dicTypeService.deleteDicTypeByCodes(code);
        int i1 = dicValueService.deleteDicValueByTypeCodes(code);
        if(i > 0){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("删除失败,请稍后重试");
        }
        return returnObject;
    }
    @RequestMapping("/settings/dictionary/type/editDicType.do")
    public String editDicType(String code, HttpServletRequest request) {
        DicType dicType = dicTypeService.selectDicTypeByCode(code);
        request.setAttribute("dicType", dicType);

        return "settings/dictionary/type/edit";
    }
    @RequestMapping("/settings/dictionary/type/saveEditDicType.do")
    @ResponseBody
    public Object saveEditDicType(DicType dicType){
        ReturnObject returnObject = new ReturnObject();
        int i = dicTypeService.updateDicType(dicType);
        if(i == 0){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("更新失败,请稍后重试");
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        }

        return returnObject;
    }
    @RequestMapping("/settings/dictionary/value/toSave.do")
    public String toSaveValue(Model model){
        List<DicType> dicTypes = dicTypeService.selectAllDicTypes();
        model.addAttribute("dicTypeList", dicTypes);
        return "settings/dictionary/value/save";
    }
    @RequestMapping("/settings/dictionary/value/saveCreateDicValue.do")
    @ResponseBody
    public Object saveCreateDicValue(DicValue dicValue){
        ReturnObject returnObject = new ReturnObject();
        String uuid = UUIDUtils.getUUID();
        dicValue.setId(uuid);
        int i = dicValueService.insertDicValue(dicValue);
        if(i == 1){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("添加失败!");
        }
        return returnObject;
    }
    @RequestMapping("/settings/dictionary/value/deleteDicValueByIds.do")
    @ResponseBody
    public Object deleteDicValueByIds(String[] id){
        int i = dicValueService.deleteDicValueByIds(id);
        ReturnObject returnObject = new ReturnObject();
        if(i == 0){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("删除失败,请稍后重试");
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        }
        return returnObject;
    }
    @RequestMapping("/settings/dictionary/value/editDicValue.do")
    public String toeditDicValue(String id,Model model){
        DicValue dicValue = dicValueService.selectByPrimaryKey(id);
        model.addAttribute("dicValue", dicValue);
        return "settings/dictionary/value/edit";
    }
    @RequestMapping("/settings/dictionary/value/saveEditDicValue.do")
    @ResponseBody
    public Object saveNewCreateDicValue(DicValue dicValue){
        int i = dicValueService.updateDicValue(dicValue);
        ReturnObject returnObject = new ReturnObject();
        if(i == 1){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("更新失败,请稍后重试");

        }
        return returnObject;
    }
}
